<?php


class PreferencesController extends GTL_Action {

    public function init( ) {
        parent::init();
        /* Initialize action controller here */
    }
    public function indexAction( ) {
        $this->_helper->redirector('form', 'preferences', '');
    }
    public function formAction( ) {
        $profileForm = new Form_PreferencesForm();
        $successmessage = "";
        $errorMessageArray = Array();
        //Load Meta Model
        $metamodel = new Model_Metadata();
        
        //Load the language array for the select box in form 
        $languageArray = $metamodel->getlanguageArray();
        $profileForm->DEFAULT_LANGUAGE->addMultiOptions($languageArray);
        
        $data = $metamodel->fetchall_meta();
        
        for($i=0;$i<count($data);$i++):
            $key=$data[$i]['mtd_key'];
            $value=$data[$i]['mtd_value'];
            $profileData[$key] = $value;
        endfor;
        $this->view->imageName = $profileData['HEADER_LOGO'];
        $this->view->form = $profileForm->populate($profileData);
       

       if ( $this->getRequest()->isPost() ) {
            $postedData = $this->_request->getPost();
            if ( !$profileForm->isValid($postedData) ) {
                $errorMessage = $profileForm->getMessages();
                foreach ($errorMessage as $_err ) {
                    foreach ($_err as $_val ) {
                        $errorMessageArray[] = $_val;
                    }
                }
            }else {
                if ( count($errorMessageArray) > 0 ) {
                }else {
                   
                    $upload = new Zend_File_Transfer_Adapter_Http();
                    foreach ($upload->getFileInfo() as $fields => $asFileInfo) {
                        if ($asFileInfo['name'] != '') {
                            $folder = $this->config->UPLOAD_PATH . "logo/";
                            $upload->setDestination($folder);
                            $originalFilename = pathinfo($asFileInfo['name']);
                            $fileName = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                            $upload->addFilter('Rename', $fileName, 'filename');
                            $upload->receive($fields);
                            
                           
                            
                        }
                    }
                     /*********update paging_per_record and code in session************** */
                    $dataUpdate = array();
                    $authData = Zend_Auth::getInstance()->getIdentity();
                    
                    
                    $authData->PAGING_PER_PAGE = $postedData['PAGE_RECORD'];
                    $dataUpdate['PAGING_PER_PAGE'] = $postedData['PAGE_RECORD'];
                   // $metamodel->save($dataUpdate, 'lgn_id');
                    /************************************************************** */
                    
                    
                        
                    
                    
                    $profileData['ADMIN_FROM_NAME'] = $postedData['ADMIN_FROM_NAME'];
                    $profileData['ADMIN_FROM'] = $postedData['ADMIN_FROM'];
                    $profileData['ADMIN_REPLY_NAME'] = $postedData['ADMIN_REPLY_NAME'];
                    $profileData['ADMIN_REPLY'] = $postedData['ADMIN_REPLY'];
                    $profileData['ADMIN_SIGN'] = $postedData['ADMIN_SIGN'];
                    $profileData['PAGE_RECORD'] = $postedData['PAGE_RECORD'];
                    $profileData['HEADER_LOGO'] = $fileName;
                    $profileData['DEFAULT_LANGUAGE'] = $postedData['DEFAULT_LANGUAGE'];
                    $metamodel->save_update_data($profileData);
                    $this->_helper->redirector('form', 'preferences', '');
                    $successmessage = "Records updated successfully";
                    
                }
            }
            
       }
        $this->view->data = $data;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $profileForm;
         
        
    }
    
}
?>
